package com.example.dormitorysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dormitorysystem.entity.Sys;
import com.example.dormitorysystem.mapper.SysMapper;
import com.example.dormitorysystem.service.SysService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吉吉王国组
 * @since 2022-11-26
 */
@Service
public class SysServiceImpl extends ServiceImpl<SysMapper, Sys> implements SysService {

    @Override
    public Map<String, Object> getOpenTime(HttpServletRequest request) {
        QueryWrapper<Sys> sysQueryWrapper = new QueryWrapper<>();
        sysQueryWrapper.eq("key_name", "start_time").or().eq("key_name", "end_time");
        return query(sysQueryWrapper);
    }

    private Map<String, Object> query(QueryWrapper<Sys> queryWrapper) {
        List<Sys> sys = baseMapper.selectList(queryWrapper);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        for(Sys s : sys) {
            stringObjectHashMap.put(s.getKeyName(), s.getKeyValue());
        }
        return stringObjectHashMap;
    }

    @Override
    public Map<String, Object> getClassLimit(HttpServletRequest request) {
        QueryWrapper<Sys> sysQueryWrapper = new QueryWrapper<>();
        sysQueryWrapper.eq("key_name", "class_limit");
        return query(sysQueryWrapper);
    }

    @Override
    public Map<String, Object> getGroupNum(HttpServletRequest request) {
        QueryWrapper<Sys> sysQueryWrapper = new QueryWrapper<>();
        sysQueryWrapper.eq("key_name", "group_limit").or().eq("key_name", "group_num");
        return query(sysQueryWrapper);
    }
}
